<?php
namespace jy_zhongyi;
defined('BY_JYA') or exit('error');
/*
	消息提醒
*/
class notice extends \plugin{
	public function notice_create2class($order,$url='',$ispay=false){
		//发送报名成功提醒通知给买方		
		$template_id=cfc('account')->get_tpl_template_id('OPENTM409786136','报名成功通知');
		$order['bodyname']=$this->getcolumn('body',array('id'=>$order['bodyid']),'name');
		$order['classname']=$this->getcolumn('class',array('id'=>$order['classid']),'title');
		$order['ispay']=$order['ispay']?'未支付':'已支付';
		$message=array(
			'first'=>array('value'=>$ispay?'报名成功通知!':'报名下单成功！',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$order['bodyname']?:'无',"color" => "#4a5077"),
			'keyword2'=>array('value'=>$order['classname']?:'无',"color" => "#4a5077"),
			'keyword3'=>array('value'=>date('Y/m/d H:i:s',$order['createtime']),"color" => "#4a5077"),	
			'keyword4'=>array('value'=>$order['ispay'],"color" => "#4a5077"),
			'remark'=>array('value'=>'请点击详情，立即完善学员信息!（如已填写，请忽略）',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_zhongyi/order/detail/id='.$order['id'],true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($order['mid'],$template_id,$message,$url);
		}
	}
	public function notice_create2fee($order,$url=''){
		//试课报名		
		$template_id=cfc('account')->get_tpl_template_id('OPENTM208020002','试听课报名通知');
		$order['bodyname']=$this->getcolumn('body',array('id'=>$order['bodyid']),'name');
		$order['lessontitle']=$this->getcolumn('lesson',array('id'=>$order['lesson_id']),'title');
		$my=$this->get('user',array('id'=>$this->order['mid']));
		$message=array(
			'first'=>array('value'=>'您好，有新的试课报名成功！',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$order['lessontitle'],"color" => "#4a5077"),
			'keyword2'=>array('value'=>date('Y/m/d H:i:s',$order['createtime']),"color" => "#4a5077"),
			'keyword3'=>array('value'=>$order['bodyname'],"color" => "#4a5077"),
			'keyword4'=>array('value'=>$my['nickname'],"color" => "#4a5077"),
			'keyword5'=>array('value'=>$my['mobile'],"color" => "#4a5077"),
			'remark'=>array('value'=>'谢谢惠顾！',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_zhongyi/order/detail/id='.$order['id'],true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($order['mid'],$template_id,$message,$url);
		}
		
	}
	public function notice_create2classworker($order,$seller_id){
		//发送报名成功通知给管理员+班长
		$template_id=cfc('account')->get_tpl_template_id('OPENTM409786136','报名成功通知');
		$order['bodyname']=$this->getcolumn('body',array('id'=>$order['bodyid']),'name');
		$order['classname']=$this->getcolumn('class',array('id'=>$order['classid']),'title');
		$order['ispay']=$order['ispay']?'未支付':'已支付';
		$message=array(
			'first'=>array('value'=>'报名成功通知!',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$order['bodyname'],"color" => "#4a5077"),
			'keyword2'=>array('value'=>$order['classname'],"color" => "#4a5077"),
			'keyword3'=>array('value'=>date('Y/m/d H:i:s',$order['createtime']),"color" => "#4a5077"),	
			'keyword4'=>array('value'=>$order['ispay'],"color" => "#4a5077"),
			'remark'=>array('value'=>'请点击详情，立即完善学员信息!（如已填写，请忽略）',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_zhongyi/order/detail/id='.$order['id'],true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($seller_id,$template_id,$message,$url);
			cfc('account')->sendtwoNotice($seller_id,$template_id,$message,$url);
		}
	}
	public function notice_create2lesson($order,$url=''){
		//试课报名给老师+管理员	
		$template_id=cfc('account')->get_tpl_template_id('OPENTM208020002','试听课报名通知');
		$order['bodyname']=$this->getcolumn('body',array('id'=>$order['bodyid']),'name');
		$order['lessontitle']=$this->getcolumn('lesson',array('id'=>$order['lesson_id']),'title');
		$my=$this->get('user',array('id'=>$this->order['mid']));
		$setting=$this->plugin_setting('basic');
		$message=array(
			'first'=>array('value'=>'您好，有新的试课报名成功！',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$order['lessontitle'],"color" => "#4a5077"),
			'keyword2'=>array('value'=>date('Y/m/d H:i:s',$order['createtime']),"color" => "#4a5077"),
			'keyword3'=>array('value'=>$order['bodyname'],"color" => "#4a5077"),
			'keyword4'=>array('value'=>$my['nickname'],"color" => "#4a5077"),
			'keyword5'=>array('value'=>$my['mobile'],"color" => "#4a5077"),
			'remark'=>array('value'=>'谢谢惠顾！',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_zhongyi/order/detail/id='.$order['id'],true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($order['mid'],$template_id,$message,$url);
			cfc('account')->sendtwoNotice($setting['adminid'],$template_id,$message,$url);
		}
	}
	public function getuptpl_data($tplid,$orderid,$type=0){
		$setting=$this->plugin_setting('basic');
		$order=$this->model('order')->get_order($orderid);
		$member=model('core/member')->detail($order['mid'],array('credit1','credit2','mobile','avatar','nickname'));
		$datas = array(
			array('name' => '平台名称', 'value' => $setting['title']?:'教育培训'),
			array('name' => '粉丝昵称', 'value' => $member['nickname']),
			array('name' => '订单号', 'value' => $order['ordersn']),
			array('name' => '订单金额', 'value' => $order['money']),
			array('name' => '科室名称', 'value' => $order['lesson']['title']),
			array('name' => '科室老师', 'value' => $order['lessonworker']['name']),
			array('name' => '班级名称', 'value' => $order['class']['title']),
			array('name' => '班主任', 'value' => $order['classworker']['name']),
			array('name' => '监护人', 'value' => $member['nickname']),
			array('name' => '监护人电话', 'value' => $member['mobile']),
			array('name' => '学员姓名', 'value' => $order['body']['name']),
			array('name' => '下单时间', 'value' => date('Y-m-d H:i', $order['createtime'])),
			array('name' => '支付时间', 'value' => date('Y-m-d H:i', $order['paytime'])),
			array('name' => '取消时间', 'value' => date('Y-m-d H:i', $order['canceltime'])),
			array('name' => '门店', 'value' => (!(empty($order['store'])) ? $order['store']['title'] : '')),
			array('name' => '门店地址', 'value' => (!(empty($order['store'])) ? $order['store']['address'] : '')),
			array('name' => '门店联系人', 'value' => (!(empty($order['store'])) ? $order['store']['realname'] . '/' . $order['store']['mobile'] : '')),
			array('name' => '门店营业时间', 'value' => (!(empty($order['store'])) ? ((empty($order['store']['worktime']) ? '全天' : $order['store']['worktime'])) : '')),
			array('name' => '学号', 'value' => $order['number']),
			array('name' => '备注信息', 'value' => $order['remark']),
			array('name' => '当前时间', 'value' => date('Y/m/d H:i:s',time())),
		);
		$tplset=model('core/account')->get_tpl_template($tplid,'jy_zhongyi');
		$content=$tplset['first'];
		$tplset['keywords']=json_decode($tplset['keywords'],true);
		foreach($tplset['keywords'] as $tv){
			$content .=$tv['value'];
		}
		$content .=$tplset['footer'];
		$content=$this->replaceTemplate($content,$datas);
		return $content;
	}
	//替换关键词
	protected function replaceTemplate($str, $datas = array())
	{
		foreach ($datas as $d ) {
			$str = str_replace('[' . $d['name'] . ']', $d['value'], $str);
		}

		return $str;
	}
}
